package itman.elm.controller;

import itman.elm.po.Orders;
import itman.elm.service.OrdersService;
import itman.elm.service.impl.OrdersServiceImpl;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.util.List;

public class OrdersController {
    //下订单（先清空当前用户购物车中当前商家的所有食品，然后生成订单和订单明细，最后返回订单号）
    // http://localhost:8080/elm/OrdersController/createOrders?userId=?&businessId=?&daId=?
    public Object createOrders(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String userId = request.getParameter("userId");
        Integer businessId = Integer.valueOf(request.getParameter("businessId"));
        Integer daId = Integer.valueOf(request.getParameter("daId"));
        OrdersService service = new OrdersServiceImpl();
        int orderId = service.createOrder(userId, businessId, daId);
        return orderId;
    }

    // http://localhost:8080/elm/OrdersController/getOrdersById?orderId=?
    public Object getOrdersById(HttpServletRequest request,HttpServletResponse response) throws Exception{
        Integer orderId = Integer.valueOf(request.getParameter("orderId"));
        OrdersService service = new OrdersServiceImpl();
        Orders orders = service.getOrdersById(orderId);
        return orders;
    }

    // http://localhost:8080/elm/OrdersController/listOrdersByUserId?userId=?
    public Object listOrdersByUserId(HttpServletRequest request,HttpServletResponse response) throws Exception{
        String userId = request.getParameter("userId");
        OrdersService service = new OrdersServiceImpl();
        List<Orders> list = service.listOrderByUserId(userId);
        return list;
    }
}
